const app = getApp()
Page({
  data: {
    message: '',
    msgitems: [],
    typeitem: [
      // { title: 'Tencent模型1', id: 1 },
      // { title: 'Tencent模型2', id: 2 }
    ],
    typeid: '',

    // sizeitem: [
    //   { title: '方图', id: 1 },
    //   { title: '竖图', id: 2 },
    //   { title: '横图', id: 3 }
    // ],
    sizeid: 1,

    styleitem: [
      // { title: '概念艺术', id: 1 },
      // { title: '油画', id: 2 },
      // { title: '水彩画', id: 3 },
      // { title: '厚涂风格', id: 4 },
    ],
    styleid: '',

    picture: '',
    imgnum: 1,
    price:''
  },
  onLoad(option) {
    let typeitem = wx.getStorageSync('typeitem');
    
    if (option.typeid) {
      this.setData({
        typeitem: typeitem,
        typeid: option.typeid,
        sizeid: option.sizeid,
        styleid: option.styleid,
        message: option.content
      })
    } else {
      this.setData({
        typeitem: typeitem,
        typeid: typeitem[0].value
      })
    }

    // 获取参数
    this.getType();
  },
  getType() {
    app.util.request({
      url: '/huahua/info',
      data: {
        type: this.data.typeid
      }
    }).then(res => {
      let items = res.data.tuijian;
      let msgitems = [];
      for (let i = 0; i < items.length; i++) {
        msgitems.push(
          { title: items[i][0], msg: items[i][1] }
        )
      }
      // console.log(msgitems)
      this.setData({
        price:res.data.price,
        styleitem: res.data.style,
        msgitems: msgitems,
        picture:''
      })
    })
  },
  getinput(e) {
    this.setData({
      message: e.detail.value
    })
  },
  msgitem(e) {
    let msg = e.currentTarget.dataset.msg;
    this.setData({
      message: msg
    })
  },
  gettype(e) {
    let id = e.currentTarget.dataset.id;
    this.setData({
      typeid: id
    })
    this.getType();
  },
  getsize(e) {
    let id = e.currentTarget.dataset.id;
    this.setData({
      sizeid: id
    })
  },
  getstyle(e) {
    let id = e.currentTarget.dataset.id;
    if (id !== '') {
      this.setData({
        styleid: id
      })
    } else {
      this.setData({
        styleid: ''
      })
    }
  },
  chooseImage() {
    const that = this
    wx.chooseMedia({
      count: 1,
      mediaType: ['image'],
      sourceType: ['album', 'camera'],
      sizeType: ['original', 'compressed'],
      success: (res) => {
        const tempFiles = res.tempFiles
        // console.log('res', res)
        app.util.upload({
          url: '/upload/image',
          filePath: tempFiles[0]['tempFilePath'],
          name: 'image'
        }).then(ret => {
          // console.log('ret', ret)
          that.setData({
            picture: ret.data.path
          })
        })
      }
    });
  },
  getnum(e) {
    let num = e.currentTarget.dataset.num;
    this.setData({
      imgnum: num
    })
  },
  submit() {
    let that = this;
    if (!this.data.message) {
      app.util.message('请输入创意详情')
      return
    }
    let change_degree = '';
    if(this.data.typeid=='baidu'){
      change_degree = 5;
    }
    app.util.request({
      url: '/huahua/huahua',
      data: {
        message: this.data.message,
        type: this.data.typeid,
        size: this.data.sizeid,
        style: this.data.styleid,
        image: this.data.picture,
        image_num: this.data.imgnum,
        change_degree: change_degree,
      }
    }).then(res => {
      app.util.message(res.message)
      setTimeout(() => {
        if (that.data.typeid == 'mj') {
          wx.navigateTo({
            url: './generate/generate?id=' + res.data+'&typeid=mj'+'&sizeid='+
            this.data.sizeid+'&styleid='+this.data.styleid+'&content='+this.data.message
          })
        } else {
          wx.reLaunch({
            url: './look/look?id=' + res.data
          })
        }
      }, 1000);
    })
  }
})